import React from 'react';
import {Table} from 'antd';
import NoData from '@components/nodata/index.jsx';

import './scss/table.scss';

class RCTable extends React.Component {

    onSelectChange = (selectedRowKeys, selectedRows) => {
        let {updateSelected} = this.props;
        updateSelected({
            selectedRowKeys,
            selectedRows
        });
        // if (selectType === 'checkbox') {
        //     updateSelected({
        //         selectedRowKeys,
        //         selectedRows
        //     });
        //     console.log(selectedRowKeys, selectedRows);
        // } else {
        //     console.log(selectedRowKeys, selectedRows);
        // }

    }

    onSelectAll = (selected, selectedRows, changeRows) => {
        // console.log(selected, selectedRows, changeRows)
    }

    onSelected = (record, selected, selectedRows) => {
        // console.log(record, selected, selectedRows)
    }

    renderTable() {
        let {selectedRowKeys, selectType, columns} = this.props;

        columns.map((item) => {
            if (!item.title) {

            } else if (item.title.indexOf('序号') > -1) {
                item.width = 100;
            } else if (item.title.indexOf('名称') > -1) {
                item.className = 'omission-240px';
                item.width = 240;
            } else if (item.title.indexOf('操作') > -1) {
                item.className = 'options';
            } else {

            }
            return null;
        });

        const rowSelection = {
            type: 'radio',
            selectedRowKeys,
            onChange: this.onSelectChange,
            // onSelect: this.onSelected,
            // onSelectAll: this.onSelectAll
        };

        if (!selectType) {
            selectType = false;
        } else if (selectType === 'checkbox') {
            rowSelection.type = 'checkbox'
        } else {
            selectType = 'radio'
        }

        return (
            <Table className='public-table'
                   bordered={false}
                   rowKey={record => record.id}
                   rowSelection={selectType ? rowSelection : null}
                   // onRow={(record, index) => {
                   //     return {
                   //         onClick: () => {
                   //         }
                   //     }
                   // }}
                   {...this.props}
            />
        )
    }

    render() {
        let {dataSource} = this.props;
        return (
            <div className="table-content">
                {this.renderTable()}
                {
                    dataSource.length ? null : <NoData/>
                }
            </div>
        )
    }
}

export default RCTable;